// pages/search_hdx/search_hdx.js
import request from '../../utils/request';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    lishi:true,
    value: '',
    consultants: [],
    articles: [],
    questions: [],
    totalConsultants: 0,
    totalArticles: 0,
    totalQuestions: 0,
    showAllConsultants: false,
    showAllArticles: false,
    showAllQuestions: false,
    timer: null
  },
  onClick(){
    console.log("年后");
    wx.navigateBack();
  },
  onQK(){
    console.log("清空历史");
    // 更改数据
    this.setData({
      lishi: false
  });
  },
  onZJ(){
    //跳转到心理专家搜索
    wx.navigateTo({
      url: '/pages/zj_hdx/zj_hdx',
    })
  },
  onWZ(){
    //跳转到心理文章搜索
    wx.navigateTo({
      url: '/pages/wz_hdx/wz_hdx',
    })
  },
  onCS(){
    //跳转到心理测试搜索
    wx.navigateTo({
      url: '/pages/cs_hdx/cs_hdx',
    })
  },
  onHD(){
    //跳转到提问回答搜索
    wx.navigateTo({
      url: '/pages/hd_hdx/hd_hdx',
    })
  },

  // 搜索框输入变化
  onSearchChange(e) {
    // 尝试多种方式获取输入值
    let value = '';
    if (e && e.detail) {
      if (typeof e.detail === 'string') {
        value = e.detail;
      } else if (e.detail.value !== undefined) {
        value = e.detail.value;
      }
    }
    
    console.log('搜索关键词:', value);
    this.setData({ value });
  
    // 清除之前的定时器，实现防抖
    if (this.data.timer) {
      clearTimeout(this.data.timer);
    }
    
    // 设置防抖定时器，500ms后执行搜索
    const timer = setTimeout(() => {
      this.search(value);
    }, 500);
    
    this.setData({ timer });
  },
  
  // 执行综合搜索
  search(keyword) {
    // 如果搜索词为空，不执行搜索
    if (!keyword || keyword === '') {
      this.setData({
        consultants: [],
        articles: [],
        questions: [],
        totalConsultants: 0,
        totalArticles: 0,
        totalQuestions: 0
      });
      return;
    }
    
    // 同时搜索三种内容
    this.searchConsultants(keyword);
    this.searchArticles(keyword);
    this.searchQuestions(keyword);
  },
  
  // 搜索咨询师
  searchConsultants(name) {
    request('/consultant/search', 'GET', {
      name: name,
      page: 1,
      pageSize: 10
    }, (res) => {
      if (res.data.code === 1) {
        const allResults = res.data.data.rows || [];
        const totalCount = allResults.length;
        
        this.setData({
          allConsultants: allResults,
          totalConsultants: totalCount,
          consultants: this.data.showAllConsultants ? allResults : (totalCount > 3 ? allResults.slice(0, 3) : allResults)
        });
      }
    });
  },
  
  // 搜索文章
  searchArticles(title) {
    request('/article/search', 'GET', {
      title: title,
      page: 1,
      pageSize: 10
    }, (res) => {
      if (res.data.code === 1) {
        const allResults = res.data.data.rows || [];
        const totalCount = allResults.length;
        
        this.setData({
          allArticles: allResults,
          totalArticles: totalCount,
          articles: this.data.showAllArticles ? allResults : (totalCount > 3 ? allResults.slice(0, 3) : allResults)
        });
      }
    });
  },
  
  // 搜索问题
  searchQuestions(title) {
    request('/questions/selectByTitle', 'GET', {
      title: title,
      pageNum: 1,
      pageSize: 10
    }, (res) => {
      if (res.data.code === 1) {
        const allResults = res.data.data.list || [];
        const totalCount = allResults.length;
        
        this.setData({
          allQuestions: allResults,
          totalQuestions: totalCount,
          questions: this.data.showAllQuestions ? allResults : (totalCount > 3 ? allResults.slice(0, 3) : allResults)
        });
      }
    });
  },
  
  // 显示全部咨询师
  showAllConsultants() {
    this.setData({
      showAllConsultants: true,
      consultants: this.data.allConsultants
    });
  },
  
  // 显示全部文章
  showAllArticles() {
    this.setData({
      showAllArticles: true,
      articles: this.data.allArticles
    });
  },
  
  // 显示全部问题
  showAllQuestions() {
    this.setData({
      showAllQuestions: true,
      questions: this.data.allQuestions
    });
  },
  
  // 点击咨询师项
  onConsultantTap(e) {
    const item = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: `/pages/teacher_detail/teacher_detail?id=${item.id}`
    });
  },
  
  // 点击文章项
  onArticleTap(e) {
    const item = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: `/pages/article_detail/article_detail?id=${item.id}`
    });
  },
  
  // 点击问题项
  onQuestionTap(e) {
    const item = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: `/pages/zt_xcj/zt_xcj?title=${encodeURIComponent(item.title)}&id=${item.id}`
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 如果有搜索参数，立即执行搜索
    if (options.keyword) {
      this.setData({ value: options.keyword });
      this.search(options.keyword);
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})